package com.iss.efi.config;

import javax.xml.ws.Endpoint;
import com.iss.efi.service.EfiWsService;

import com.iss.efi.util.SymmetricEncryptionUtil;
import org.apache.cxf.Bus;
import org.apache.cxf.bus.spring.SpringBus;
import org.apache.cxf.jaxws.EndpointImpl;
import org.apache.cxf.transport.servlet.CXFServlet;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


import javax.annotation.Resource;

@Configuration
public class WebServiceConfig {

    @Resource
    private EfiWsService efiWsService;


    @Value("${wsSecret:2a92fdbb25ef4b17ba3030fa6f512765}")
    private String secret;

    /**
     * 注入servlet  bean name不能dispatcherServlet 否则会覆盖dispatcherServlet
     * @return
     */
    @Bean(name = "cxfServlet")
    public ServletRegistrationBean cxfServlet() {
        return new ServletRegistrationBean(new CXFServlet(),"/*");
    }

    @Bean(name = Bus.DEFAULT_BUS_ID)
    public SpringBus springBus() {
        return new SpringBus();
    }


    @Bean
    public Endpoint endpoint() {
        SymmetricEncryptionUtil.setKeyValue(secret);
        EndpointImpl endpoint = new EndpointImpl(springBus(), efiWsService);
        endpoint.publish("/efiWsService");
        return endpoint;
    }

}
